//alert('Cam');

function IsNumeric(sText){
   //var ValidChars = "0123456789.";
    var ValidChars = "0123456789.";
    var IsNumber=true;
	var Char;
	if(sText){ 
		for (i = 0; i < sText.length && IsNumber == true; i++){ 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) 
			{
				//IsNumber = false;
				return false;
			}
		}
		   //return IsNumber;
		   return true;
	}
	return false;
}
function checkDecimal(obj, objStr){
//formart number like 0.0000
	var Num = objStr;
	//alert(Num);
	var test = IsNumeric(Num);
	if(test == false){
		Num ="0";
	}
	if(parseFloat(Num)>=100){
		Num = "0";
		end = "0000";
		//alert ('Number must less than 100');
	}
	var newNum = "";
	var newNum2 = ""; // Clear New Num
	var count = 0; 
	
	if (Num.indexOf('.') != -1){ //number ends with a decimal point

		if (Num.indexOf('.') == Num.length-1){
		
			Num += "0000";
		
		}
		
		if ((Num.indexOf('.')) == (Num.length-2)){ //number ends with a single digit
		
			Num += "000";
		
		}
		if ((Num.indexOf('.')) == (Num.length-3)){ //number ends with a two digit
		
			Num += "00";
		
		}
		if ((Num.indexOf('.')) == (Num.length-4)){ //number ends with a three digit
		
			Num += "0";
		
		}
		var a = Num.split(".");

		Num = a[0]; //the part we will commify
		var end = a[1] //the decimal place we will ignore and add back later
				
		if(!Num){
			Num = "0";
		}else{
			if(parseInt(Num)>100){
				Num = "0";
				end = "0000";
				//alert ('Number must less than 100');
			}
		}
	}else {
		var end = "0000";
	} 
	newNum2 = Num + "." + end; 
	obj.value = newNum2;
}

function checkDecimal2(obj, objStr){
//formart number like 0.0000
	var Num = objStr;
	//alert(Num);
	var test = IsNumeric(Num);
	if(test == false){
		Num ="0";
	}
	if(parseFloat(Num)>=1000000){
		Num = "0";
		end = "0000";
		//alert ('Number must less than 100');
	}
	var newNum = "";
	var newNum2 = ""; // Clear New Num
	var count = 0; 
	
	if (Num.indexOf('.') != -1){ //number ends with a decimal point

		if (Num.indexOf('.') == Num.length-1){
		
			Num += "0000";
		
		}
		
		if ((Num.indexOf('.')) == (Num.length-2)){ //number ends with a single digit
		
			Num += "000";
		
		}
		if ((Num.indexOf('.')) == (Num.length-3)){ //number ends with a two digit
		
			Num += "00";
		
		}
		if ((Num.indexOf('.')) == (Num.length-4)){ //number ends with a three digit
		
			Num += "0";
		
		}
		var a = Num.split(".");

		Num = a[0]; //the part we will commify
		var end = a[1] //the decimal place we will ignore and add back later
				
		if(!Num){
			Num = "0";
		}else{
			if(parseInt(Num)>1000000){
				Num = "0";
				end = "0000";
				//alert ('Number must less than 100');
			}
		}
	}else {
		var end = "0000";
	} 
	newNum2 = Num + "." + end; 
	obj.value = newNum2;
}

function checkDecimal3(obj, objStr){
//formart number like 0.0000
	var Num = objStr;
	//alert(Num);
	var test = IsNumeric(Num);
	if(test == false){
		Num ="0";
	}
	if(parseFloat(Num)>=100){
		Num = "0";
		end = "00";
		//alert ('Number must less than 100');
	}
	var newNum = "";
	var newNum2 = ""; // Clear New Num
	var count = 0; 
	
	if (Num.indexOf('.') != -1){ //number ends with a decimal point

		if (Num.indexOf('.') == Num.length-1){
		
			Num += "00";
		
		}
		
		if ((Num.indexOf('.')) == (Num.length-2)){ //number ends with a single digit
		
			Num += "0";
		
		}	
		var a = Num.split(".");

		Num = a[0]; //the part we will commify
		var end = a[1] //the decimal place we will ignore and add back later
				
		if(!Num){
			Num = "0";
		}else{
			if(parseInt(Num)>100){
				Num = "0";
				end = "00";
				//alert ('Number must less than 100');
			}
		}
	}else {
		var end = "00";
	} 
	newNum2 = Num + "." + end; 
	obj.value = newNum2;
}

function checkDecimal4(obj, objStr){
//formart number like 0.0000
	var Num = objStr;
	//alert(Num);
	var test = IsNumeric(Num);
	if(test == false){
		Num ="0";
	}
	if(parseFloat(Num)>=1000000){
		Num = "0";
		end = "00";
		//alert ('Number must less than 100');
	}
	var newNum = "";
	var newNum2 = ""; // Clear New Num
	var count = 0; 
	
	if (Num.indexOf('.') != -1){ //number ends with a decimal point

		if (Num.indexOf('.') == Num.length-1){
		
			Num += "00";
		
		}
		
		if ((Num.indexOf('.')) == (Num.length-2)){ //number ends with a single digit
		
			Num += "0";
		
		}	
		var a = Num.split(".");

		Num = a[0]; //the part we will commify
		var end = a[1] //the decimal place we will ignore and add back later
				
		if(!Num){
			Num = "0";
		}else{
			if(parseInt(Num)>1000000){
				Num = "0";
				end = "00";
				//alert ('Number must less than 100');
			}
		}
	}else {
		var end = "00";
	} 
	newNum2 = Num + "." + end; 
	obj.value = newNum2;
}

function checkDecimal7(obj, objStr){
//formart number like 0.0000
	var Num = objStr;
	//alert(Num);
	var test = IsNumeric(Num);
	if(test == false){
		Num ="0";
	}
	if(parseFloat(Num)>=1000000){
		Num = "0";
		end = "0000000";
		//alert ('Number must less than 100');
	}
	var newNum = "";
	var newNum2 = ""; // Clear New Num
	var count = 0; 
	
	if (Num.indexOf('.') != -1){ //number ends with a decimal point

		if (Num.indexOf('.') == Num.length-1){
		
			Num += "0000000";
		
		}
		
		if ((Num.indexOf('.')) == (Num.length-2)){ //number ends with a single digit
		
			Num += "000000";
		
		}	
		
		if ((Num.indexOf('.')) == (Num.length-3)){ //number ends with a single digit
		
			Num += "00000";
		
		}	
		
		if ((Num.indexOf('.')) == (Num.length-4)){ //number ends with a single digit
		
			Num += "0000";
		
		}	
		
		if ((Num.indexOf('.')) == (Num.length-5)){ //number ends with a single digit
		
			Num += "000";
		
		}	
		
		if ((Num.indexOf('.')) == (Num.length-6)){ //number ends with a single digit
		
			Num += "00";
		
		}	
		
		if ((Num.indexOf('.')) == (Num.length-7)){ //number ends with a single digit
		
			Num += "0";
		
		}	
		
		var a = Num.split(".");

		Num = a[0]; //the part we will commify
		var end = a[1] //the decimal place we will ignore and add back later
				
		if(!Num){
			Num = "0";
		}else{
			if(parseInt(Num)>1000000){
				Num = "0";
				end = "0000000";
				//alert ('Number must less than 100');
			}
		}
	}else {
		var end = "0000000";
	} 
	newNum2 = Num + "." + end; 
	obj.value = newNum2;
}


function checkCurrency20(obj, objStr){
//formart number like 0.0000
	var Num = objStr;
	//alert(Num);
	var test = IsNumeric(Num);
	if(test == false){
		Num ="0";
	}
	//alert(Num);
	if(parseFloat(Num)>=1000000000000000000){
		Num = "0";
		end = "00";
		//alert ('Number must less than 100');
	}
	var newNum = "";
	var newNum2 = ""; // Clear New Num
	var count = 0; 
	
	if (Num.indexOf('.') != -1){ //number ends with a decimal point

		if (Num.indexOf('.') == Num.length-1){
		
			Num += "00";
		
		}
		
		if ((Num.indexOf('.')) == (Num.length-2)){ //number ends with a single digit
		
			Num += "0";
		
		}	
		var a = Num.split(".");

		Num = a[0]; //the part we will commify
		var end = a[1] //the decimal place we will ignore and add back later
				
		if(!Num){
			Num = "0";
		}else{
			if(parseInt(Num)>1000000000000000000){
				Num = "0";
				end = "00";
				//alert ('Number must less than 100');
			}
		}
	}else {
		var end = "00";
	} 
	newNum2 = Num + "." + end; 
	obj.value = newNum2;
}

function stripDuplicateChars(str, strip, n, s){
	//str: so truyen vao
	//strip: dau ',' hoac '.' can cat di
	//n:  so strip cho phep ton tai
	//s=0
		var count=0;
		var stripped=str.substring(0, s);
		var chr;
		for (var i=s; i<str.length; i++){ 
			chr = str.substring(i, i+1);
			if (chr == strip){ 
				count++; 
				if (count<n+1){ 
					stripped = stripped +chr;
				}
			}else {
				stripped = stripped + chr;
			}
		} 
		return stripped;
	} 

//Dua so ve dang 0.00
function formatNumber(obj, objStr){//obj object, objStr: object value
	var Num = objStr;
	var trueNum = stripDuplicateChars(Num,',',0,0);
	Num = trueNum;
	//alert(Num);
	var test = IsNumeric(Num);
	if(test == false){
		Num ="0";
	}
	var newNum = "";
	var newNum2 = ""; // Clear New Num
	var count = 0; 
	
	if (Num.indexOf('.') != -1){ //number ends with a decimal point

		if (Num.indexOf('.') == Num.length-1){
		
			Num += "00";
		
		}
		
		if ((Num.indexOf('.')) == (Num.length-2)){ //number ends with a single digit
		
			Num += "0";
		
		}
		
		var a = Num.split(".");

		Num = a[0]; //the part we will commify
		var end = a[1] //the decimal place we will ignore and add back later
				
		if(!Num){
			Num = "0";
		}
		if(Num.length>15){//Phan nguyen lon nhat la 14 chu so
			Num="0";
			end="00";
		}
		
		if(parseInt(end)>99){
			var stripped=end.substring(0, 2);
			end = stripped;	
		}			
	}else {
		var end = "00";
		if(Num.length>15){
			Num="0";
		}
		
	} 
	for (var k = Num.length-1; k >= 0; k--){
	
		var oneChar = Num.charAt(k);
		
		if (count == 3){
		
			newNum += ",";
			
			newNum += oneChar;
			
			count = 1;
			
			continue;
		
		}
		
		else {
		
			newNum += oneChar;
			
			count ++;
		
		}
		
	} //but now the string is reversed!
		
		
	//re-reverse the string
	
	for (var k = newNum.length-1; k >= 0; k--){
	
		var oneChar = newNum.charAt(k);
	
		newNum2 += oneChar;

	}
	
	newNum2 = newNum2 + "." + end; 
	//newNum2 = Num + "." + end; 
	obj.value = newNum2;
}

function isFloatingPointNumber(val){
		var fpnum = /^-{0,1}\d*\.{0,1}\d*$/g;
		if (fpnum.test(val)){
			return true;
		} else {
			return false;
		}
	}
	
	function allowOnlyFloatingPointNumbers(textbox, val){
		//alert(val);
	  val = val.replace(/[^0-9.]/g, ''); // strip non-digit chars
	  val = stripDuplicateChars(val, '.', 1, 0); // strip excess decimals
	  
	  val = stripDuplicateChars(val, '-', 0, 0); // strip excess minus signs
	  textbox.value = val; // replace textbox value
	}
/*
function checkvalue(textbox, val, maxval){// obj, obj value, string for max value to reach
	val = stripDuplicateChars(Num,',',0,0);
	if (val.indexOf('.') != -1){
		if(val.indexOf(".")>maxval.length){
			textbox.value = "0";
		}
	}else{
		if(val.length > maxval.length){
			textbox.value = "0";	
		}
	}
	formatNumber(textbox, textbox.value);
}
*/
//format number 0.0000
function formatNumber2(obj, objStr, maxval, num_decimal){//obj object, objStr: object value
	var Num = objStr;
	var trueNum = stripDuplicateChars(Num,',',0,0);
	
	Num = trueNum;
	//alert(Num);
	var test = IsNumeric(Num);
	if(test == false){
		Num ="0";
	}
	var newNum = "";
	var newNum2 = ""; // Clear New Num
	var count = 0;
	var j = 10;//bien nay dieu kien mang deci
	var i = 0;
	var deci = new Array(j);// mang deci co j phan tu
	deci[0] = "";
	deci[1] = "0";
	deci[2] = "00";
	deci[3] = "000";
	deci[4] = "0000";
	deci[5] = "00000";
	deci[6] = "000000";
	deci[7] = "0000000";
	deci[8] = "00000000";
	deci[9] = "000000000";
	var str = "";
	if (Num.indexOf('.') != -1){ //number ends with a decimal point
		var l_num = Num.length;
		var pos_decimal = Num.indexOf('.');
		var temp = l_num - pos_decimal;
		while(i!=temp){
			j--;
			i++;
		}
		str = Num + deci[j];
		Num = str;
		
		var a = Num.split(".");

		Num = a[0]; //the part we will commify
		var end = a[1] //the decimal place we will ignore and add back later
		
		if(!Num){
			Num = "0";
		}
		if(Num.length>maxval){
			Num="0";
			end = deci[num_decimal];
		}
		
		if(end.length>num_decimal){
			var stripped=end.substring(0, num_decimal);
			end = stripped;	
		}			
	}else {
		var end = deci[num_decimal];
		if(Num.length>maxval){
			Num="0";
		}
	} 
	for (var k = Num.length-1; k >= 0; k--){
	
		var oneChar = Num.charAt(k);
		
		if (count == 3){
		
			newNum += ",";
			
			newNum += oneChar;
			
			count = 1;
			
			continue;
		
		}
		
		else {
		
			newNum += oneChar;
			
			count ++;
		
		}
		
	} //but now the string is reversed!
		
		
	//re-reverse the string
	
	for (var k = newNum.length-1; k >= 0; k--){
	
		var oneChar = newNum.charAt(k);
	
		newNum2 += oneChar;

	}
	
	if(end){
		newNum2 = newNum2 + "." + end; 
	}	
	//newNum2 = Num + "." + end; 
	obj.value = newNum2;
}
function formatNumbernegative(obj, objStr, maxval, num_decimal){//obj object, objStr: object value

    var Num = objStr;
    var negative = 0;
	
	Num = Num.replace(/,/g,"");
    if(Num*1<0){
     Num = Num.replace('-','');
     negative = 1;
    }
	
    var trueNum = stripDuplicateChars(Num,',',0,0);
    
    Num = trueNum;
	//alert("asldkjl");
    var test = IsNumeric(Num);
    if(test == false){
     Num ="0";
    }
    var newNum = "";
    var newNum2 = ""; // Clear New Num
    var count = 0;
    var j = 10;//bien nay dieu kien mang deci
    var i = 0;
    var deci = new Array(j);// mang deci co j phan tu
    deci[0] = "";
    deci[1] = "0";
    deci[2] = "00";
    deci[3] = "000";
    deci[4] = "0000";
    deci[5] = "00000";
    deci[6] = "000000";
    deci[7] = "0000000";
    deci[8] = "00000000";
    deci[9] = "000000000";
    var str = "";
    if (Num.indexOf('.') != -1){ //number ends with a decimal point
     var l_num = Num.length;
     var pos_decimal = Num.indexOf('.');
     var temp = l_num - pos_decimal;
     while(i!=temp){
      j--;
      i++;
     }
     str = Num + deci[j];
     Num = str;
     
     var a = Num.split(".");
   
     Num = a[0]; //the part we will commify
     var end = a[1]; //the decimal place we will ignore and add back later
     
     if(!Num){
      Num = "0";
     }
     if(Num.length>maxval){
      Num="0";
      end = deci[num_decimal];
     }
     
     if(end.length>num_decimal){
      var stripped=end.substring(0, num_decimal);
      end = stripped; 
     }   
    }else {
     var end = deci[num_decimal];
     if(Num.length>maxval){
      Num="0";
     }
    } 
    for (var k = Num.length-1; k >= 0; k--){
    
     var oneChar = Num.charAt(k);
     
     if (count == 3){
     
      newNum += ",";
      
      newNum += oneChar;
      
      count = 1;
      
      continue;
     
     }
     
     else {
     
      newNum += oneChar;
      
      count ++;
     
     }
     
    } //but now the string is reversed!
     
     
    //re-reverse the string
    
    for (var k = newNum.length-1; k >= 0; k--){
    
     var oneChar = newNum.charAt(k);
    
     newNum2 += oneChar;
   
    }
    
    if(end){
     newNum2 = newNum2 + "." + end; 
    } 
    //newNum2 = Num + "." + end; 
    if(negative == 1){
     newNum2 = "-"+newNum2;
    }
    obj.value = newNum2;
 }
function numberOnly(evt) {
	evt = (evt) ? evt : ((window.event) ? event : null);
	if (evt) {
	   var elem = (evt.target) ? evt.target : 
		  ((evt.srcElement) ? evt.srcElement : null);	   
	   if (elem) {
		   //var charCode = (evt.charCode) ? evt.charCode : 
			//   ((evt.which) ? evt.which : evt.keyCode);
		   var charCode = evt.which;
		   //alert(charCode);	   
		   if ((charCode < 32 ) || 
			    charCode == 37 || charCode == 39 || charCode == 46 ||
			   (charCode > 44 && charCode < 47) || 
			   (charCode > 47 && charCode < 58)) {
			   return true;
		   } else {
			   return false;
		   }
	   }
	}		
}


function numberOnly2(evt) {
	evt = (evt) ? evt : ((window.event) ? event : null);
	if (evt) {
	   var elem = (evt.target) ? evt.target : 
		  ((evt.srcElement) ? evt.srcElement : null);	   
	   if (elem) {
		   //var charCode = (evt.charCode) ? evt.charCode : 
			//   ((evt.which) ? evt.which : evt.keyCode);
		   var charCode = evt.which;
		   //alert(charCode);	   
		   if ((charCode > 47 && charCode <= 58) || (charCode == 0) || (charCode == 8)) {
			   return true;
		   } else {
			   return false;
		   }
	   }
	}		
}

function numberOnly3(evt) {
	evt = (evt) ? evt : ((window.event) ? event : null);
	if (evt) {
	   var elem = (evt.target) ? evt.target : 
		  ((evt.srcElement) ? evt.srcElement : null);	   
	   if (elem) {
		   //var charCode = (evt.charCode) ? evt.charCode : 
			//   ((evt.which) ? evt.which : evt.keyCode);
		   var charCode = evt.which;
		   //alert(charCode);	   
		   if ((charCode >= 47 && charCode < 58) || (charCode == 0) || (charCode == 8)) {
			   return true;
		   } else {
			   return false;
		   }
	   }
	}		
}


function formatNumberNull(obj, objStr, maxval, num_decimal){//obj object, objStr: object value
	var Num = objStr;
	var trueNum = stripDuplicateChars(Num,',',0,0);
	
	Num = trueNum;
	//alert(Num);
	var test = IsNumeric(Num);
	if(test == false){
		Num ="0";
	}
	var newNum = "";
	var newNum2 = ""; // Clear New Num
	var count = 0;
	var j = 10;//bien nay dieu kien mang deci
	var i = 0;
	var deci = new Array(j);// mang deci co j phan tu
	deci[0] = "";
	deci[1] = "0";
	deci[2] = "00";
	deci[3] = "000";
	deci[4] = "0000";
	deci[5] = "00000";
	deci[6] = "000000";
	deci[7] = "0000000";
	deci[8] = "00000000";
	deci[9] = "000000000";
	var str = "";
	if (Num.indexOf('.') != -1){ //number ends with a decimal point
		var l_num = Num.length;
		var pos_decimal = Num.indexOf('.');
		var temp = l_num - pos_decimal;
		while(i!=temp){
			j--;
			i++;
		}
		str = Num + deci[j];
		Num = str;
		
		var a = Num.split(".");

		Num = a[0]; //the part we will commify
		var end = a[1] //the decimal place we will ignore and add back later
		
		if(!Num){
			Num = "0";
		}
		if(Num.length>maxval){
			Num="0";
			end = deci[num_decimal];
		}
		
		if(end.length>num_decimal){
			var stripped=end.substring(0, num_decimal);
			end = stripped;	
		}			
	}else {
		var end = deci[num_decimal];
		if(Num.length>maxval){
			Num="0";
		}
	} 
	for (var k = Num.length-1; k >= 0; k--){
	
		var oneChar = Num.charAt(k);
		
		if (count == 3){
		
			newNum += ",";
			
			newNum += oneChar;
			
			count = 1;
			
			continue;
		
		}
		
		else {
		
			newNum += oneChar;
			
			count ++;
		
		}
		
	} //but now the string is reversed!
		
		
	//re-reverse the string
	
	for (var k = newNum.length-1; k >= 0; k--){
	
		var oneChar = newNum.charAt(k);
	
		newNum2 += oneChar;

	}
	
	if(end){
		newNum2 = newNum2 + "." + end; 
	}	
	//newNum2 = Num + "." + end; 
	if(newNum2==0){
		obj.value='';
	}else{
		obj.value = newNum2;
	}
}